package com.bicon.botu.account.controller;

import com.bicon.botu.account.service.IRoleResourcesService;
import com.bicon.botu.core.base.common.entity.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 角色资源增删改查接口类
 *
 * @author peizp
 * @date 2017/6/14 0012 下午 2:47
 */
@Slf4j
@RestController
@RequestMapping("/V1.0.0/roleResource")
public class RoleResourceController {

    @Autowired
    private IRoleResourcesService iRoleResourcesService;

    /**
     * 增加角色资源关联
     *
     * @param roleId      角色Id
     * @param resourcesId 资源id,可多个一起传。双井号隔开“##”
     * @param type        账户类型：0：前台账户，1：后台账户
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult add(@RequestParam String resourcesId, @RequestParam String roleId, @RequestParam String type) {
        String[] resourceIds = resourcesId.split("##");
        BaseResult baseResult = iRoleResourcesService.saveRoleResourceIds(roleId, resourceIds, type);
        return baseResult;
    }

    /**
     * 删除角色资源
     *
     * @param roleId 角色ID
     * @param type   账户类型：0：前台账户，1：后台账户
     * @return
     */
    @RequestMapping(value = "/delete")
    public BaseResult delete(@RequestParam String roleId, @RequestParam String type) {
        return iRoleResourcesService.deleteRoleResourcesByRoleId(roleId, type);
    }

    /**
     * 查询角色下的资源(树形结构)
     *
     * @param roleId 角色Id
     * @param type   账户类型：0：前台账户，1：后台账户
     * @return
     */
    @RequestMapping(value = "/selectResourcesByRoleId")
    public BaseResult selectRoleIdsByAccountId(@RequestParam String roleId, @RequestParam String type) {
        return iRoleResourcesService.findRolesResources(roleId, type);
    }

    /**
     * 初始化角色-资源关系到redis
     *
     * @return
     */
    @RequestMapping(value = "/initRoleResources", method = RequestMethod.GET)
    public BaseResult initRoleResources() {
        return iRoleResourcesService.initRoleResources();
    }

}
